package dao;

import domain.Employee;
import utils.DateAndString;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version 1.0
 * @autho 3505
 * @date 2018/6/1
 */
public class EmployeeDaoList implements BasicDao {
    private List<Employee> list=new ArrayList<>();

    public EmployeeDaoList() {

    }


    @Override
    //增加一个对象
    public int add(Employee employee) {

            list.add(employee);
            return 1;


    }

    @Override
    //输入姓名修改工资
    public int modify(Employee employee) {
        Iterator<Employee> it=list.iterator();
        int flag=0;
        while (it.hasNext()){
            Employee employee1=it.next();
            if (employee1.getEmployeeName().equals(employee.getEmployeeName())){
                employee1.setSalary(employee.getSalary());
                flag=1;
                break;
            }
        }
        if(flag==0){
            System.out.println("查无此人");
        }else {
            System.out.println("修改成功");
            return 1;
        }
        return 0;
    }

    @Override
    //输入姓名，删除叫这个名字的人
    public int delete(String name) {

        Iterator<Employee> it=list.iterator();
        int flag=0;
        while (it.hasNext()){
            Employee employee=it.next();
            if (employee.getEmployeeName().equals(name)){
                list.remove(employee);
                flag=1;
                break;
            }
        }
        if(flag==0){
            System.out.println("查无此人");
            return 0;
        }else {
            System.out.println("删除成功");
            return 1;
        }


    }

    @Override
    //输入姓名，输出这个人的信息
    public Employee searchOne(String name) {
        if(list.size()!=0){
            Iterator<Employee> it=list.iterator();
            Employee e1=new Employee();
            while (it.hasNext()){
                Employee employee=it.next();
                if (employee.getEmployeeName().equals(name)){
                    System.out.println(employee);

                    break;
                }
            }
            return e1;

        }
        System.out.println("查无此人");
        return null;
    }

    @Override
    public List<Employee> searchAll() {
        List<Employee> list1=new ArrayList<>();
        Iterator<Employee> it=list.iterator();
        int flag=0;
        while (it.hasNext()){
            Employee employee=it.next();
            //System.out.println(employee);
            list1.add(employee);
        }
        return list1;
    }

}
